# 第03章 简单工厂
# 3.0 GOF23设计模式
# 3.0.1 创建型:关于如何创建实例
可以分为两组:类创建和对象创建,类创建在实例化过程中有效的使用类之间的继承关系,对象创建则使用代理来完成其任务
- 抽象工厂 (Abstract Factory)
- 构造器 (Builder Pattern)
- 工厂方法 (Factory Method pattern)
- 原型 (Prototype pattern)
- 单例模式 (Singleton pattern)
# 3.0.2 结构型:关于类及对象复合关系
- 适配器(Adapter pattern)
- 桥接(Bridge pattern)
- 组合(Composite pattern)
- 装饰(Decorator pattern)
- 外观(Façade pattern)
- 享元(Flyweight pattern)
- 代理(Proxy pattern)
# 3.0.3 行为型:关于对象之间如何通讯
- 职责链(Chain-of-responsibility pattern)
- 命令(Command pattern)
- 翻译器(Interpreter pattern)
- 迭代器(Iterator pattern)
- 中介者(Mediator pattern)
- 回忆(Memento pattern)
- 观察者(Observer pattern)
- 状态机(State pattern)
- 策略(Strategy pattern)
- 模板方法(Template method pattern)
- 参观者(Visitor)
# 3.1 定义与类型
定义:由一个工厂对象决定创建出哪一种产品类的实例;
类型:创建型,但不属于GOF23种设计模式
# 3.2 使用场景
工厂类负责创建的对象比较少;
客户端(应用层)只知道传入工厂类的参数,对于如何创建对象(逻辑)不关心;
# 3.3 优缺点
优点:只需要传入一个正确的参数,就可以获取你所需要的对象,而无需知道其创建细节;
缺点:工厂类的职责相对过重,增加新的产品需要修改工厂类的判断逻辑,违背开闭原则;
# 3.4 不使用简单工厂创建
public abstract class Video {
public abstract void produce();
}
public class JavaVideo extends Video {
@Override
public void produce() {
System.out.println("录制Java课程视频");
}
}
public class PythonVideo extends Video {
@Override
public void produce() {
System.out.println("录制Python课程视频");
}
}
public class Test {
public static void main(String[] args) {
Video video = new JavaVideo();
video.produce();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 3.4 使用简单工厂创建(名称)
public abstract class Video {
public abstract void produce();
}
public class JavaVideo extends Video {
@Override
public void produce() {
System.out.println("录制Java课程视频");
}
}
public class PythonVideo extends Video {
@Override
public void produce() {
System.out.println("录制Python课程视频");
}
}
public class VideoFactory {
public Video getVideo(String type){
if("java".equalsIgnoreCase(type)){
return new JavaVideo();
}else if("python".equalsIgnoreCase(type)){
return new PythonVideo();
}
return null;
}
}
public class Test {
public static void main(String[] args) {
VideoFactory videoFactory = new VideoFactory();
Video video = videoFactory.getVideo("java");
if(video == null){
return;
}
video.produce();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 3.5 使用简单工厂创建(反射)
public abstract class Video {
public abstract void produce();
}
public class JavaVideo extends Video {
@Override
public void produce() {
System.out.println("录制Java课程视频");
}
}
public class PythonVideo extends Video {
@Override
public void produce() {
System.out.println("录制Python课程视频");
}
}
// 使用反射
public class VideoFactory {
public Video getVideo(Class c){
Video video = null;
try {
video = (Video) Class.forName(c.getName()).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return video;
}
}
public class Test {
public static void main(String[] args) {
VideoFactory videoFactory = new VideoFactory();
Video video = videoFactory.getVideo(JavaVideo.class);
if(video == null){
return;
}
video.produce();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 3.6 简单工厂JDK源码解析
# 3.6.1 Calendar.java中的getInstance方法
private static Calendar createCalendar(TimeZone zone,Locale aLocale){
CalendarProvider provider =
LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)
.getCalendarProvider();
if (provider != null) {
try {
return provider.getInstance(zone, aLocale);
} catch (IllegalArgumentException iae) {
// fall back to the default instantiation
}
}
Calendar cal = null;
if (aLocale.hasExtensions()) {
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype != null) {
switch (caltype) {
case "buddhist":
cal = new BuddhistCalendar(zone, aLocale);
break;
case "japanese":
cal = new JapaneseImperialCalendar(zone, aLocale);
break;
case "gregory":
cal = new GregorianCalendar(zone, aLocale);
break;
}
}
}
if (cal == null) {
if (aLocale.getLanguage() == "th"
&& aLocale.getCountry() == "TH") {
cal = new BuddhistCalendar(zone, aLocale);
} else if (aLocale.getVariant() == "JP"
&& aLocale.getLanguage() == "ja"
&& aLocale.getCountry() == "JP") {
cal = new JapaneseImperialCalendar(zone, aLocale);
} else {
cal = new GregorianCalendar(zone, aLocale);
}
}
return cal;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 3.6.2 Logback的LoggerFactory中的getLogger方法
public static Logger getLogger(String name) {
ILoggerFactory iLoggerFactory = getILoggerFactory();
return iLoggerFactory.getLogger(name);
}
1
2
3
4
2
3
4
查看第一个实现类的实现:
ch.qos.logback.classic.LoggerContext#getLogger(java.lang.String)
这就是反射式的简单工厂
public final Logger getLogger(Class clazz) {
return this.getLogger(clazz.getName());
}
public final Logger getLogger(String name) {
if (name == null) {
throw new IllegalArgumentException("name argument cannot be null");
} else if ("ROOT".equalsIgnoreCase(name)) {
return this.root;
} else {
int i = 0;
Logger logger = this.root;
Logger childLogger = (Logger)this.loggerCache.get(name);
if (childLogger != null) {
return childLogger;
} else {
int h;
do {
h = LoggerNameUtil.getSeparatorIndexOf(name, i);
String childName;
if (h == -1) {
childName = name;
} else {
childName = name.substring(0, h);
}
i = h + 1;
synchronized(logger) {
childLogger = logger.getChildByName(childName);
if (childLogger == null) {
childLogger = logger.createChildByName(childName);
this.loggerCache.put(childName, childLogger);
this.incSize();
}
}
logger = childLogger;
} while(h != -1);
return childLogger;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 3.7 OTA项目中的简单工厂模式运用
根据传入的策略名称的不同选择不同的策略进行执行
EnPackagePolicyType.java
/**
* 包升级策略类型枚举
*
* @author wangchao
* @create 2018-07-11 18:57
*/
public enum EnPackagePolicyType {
TUID("TUID","TUID"),
DeviceGroup("DeviceGroup","设备分组"),
Area("Area","地区升级"),
CarSeries("CarSeries","车系升级"),
Phone("Phone","手机升级"),
All("All","全部策略");
private String value;
private String description;
public String getValue() {
return this.value;
}
public String getDescription() {
return this.description;
}
EnPackagePolicyType(String value, String description) {
this.value = value;
this.description = description;
}
public static String getDescriptionByValue(String value) {
String description = "";
EnPackagePolicyType[] arr$ = values();
int len$ = arr$.length;
for(int i$ = 0; i$ < len$; ++i$) {
EnPackagePolicyType source = arr$[i$];
if (StringUtils.pathEquals(source.value, value)) {
description = source.getDescription();
break;
}
}
return description;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
OtaPackageManagerImpl.java
/**
* 包管理实现
*
* @author wangchao
* @create 2018-07-10 16:18
*/
@Component("otaPackageManager")
public class OtaPackageManagerImpl implements IOtaPackageManager {
/**
* 收集系统中所有的{@link IPackagePolicyHandler}接口实现
*/
@Autowired
private Map<String, IPackagePolicyHandler> packagePolicyHandlerMap;
/*省略其他不相关带代码*/
/**
* 确定终端需要升级的包
*
* @param packagePos 比当前版本高的上架中的全量包列表
* @return
*/
public Message<OtaPackagePo> getTargetUpgradePackage(
List<OtaPackagePo> packagePos, UpgradeCheckParam check) {
Message<OtaPackagePo> message = new Message<>();
message.setSuccess(false);
for (OtaPackagePo packagePo : packagePos) {
// 使用packageId查找包下挂的所有策略
Message<List<OtaPackagePolicyPo>> packagePolicyListMsg = this
.getPackagePolicyList(packagePo.getPackageId());
if (packagePolicyListMsg.getSuccess()) {
List<OtaPackagePolicyPo> data = packagePolicyListMsg.getData();
for (OtaPackagePolicyPo datum : data) {
// 调用工厂分配给不同的处理器执行
IPackagePolicyHandler handler = packagePolicyHandlerMap
.get(datum.getPolicyType().toLowerCase()
+ "PolicyHandler");
if (null != handler) {
Message<String> policyMatchMessage = handler
.matchPolicy(datum, check);
if (policyMatchMessage.getSuccess()) {
message.setData(packagePo);
message.setSuccess(true);
return message;
}
}
}
}
}
message.setMsg("不在任一升级策略范围内");
return message;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
IPackagePolicyHandler.java
/**
* 包策略处理器
*
* @author wangchao
* @create 2018-07-10 14:32
*/
public abstract class IPackagePolicyHandler {
@Autowired
private IOtaPackageService otaPackageService;
public void setOtaPackageService(IOtaPackageService otaPackageService) {
this.otaPackageService = otaPackageService;
}
/**
* 包策略推行
*
* @param otaPackagePolicyPo 包策略对象
* @param check
* @return
*/
public abstract Message<String> matchPolicy(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check);
/**
* 策略匹配 使用绝对是啊金方式
*
* @param otaPackagePolicyPo 包策略对象
* @param check
* @return
*/
public abstract Message<String> matchPolicyAbsolute(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check);
/**
* 是否满足生效时间
*/
public boolean isEffect(OtaPackagePolicyPo otaPackagePolicyPo) {
boolean isEffect = false;
// 获取包的上架(此时已经确定该包已经上架)
Message<OtaPackagePo> otaPackageMsg = otaPackageService.getByPrimaryKey(otaPackagePolicyPo.getPackageId());
if (otaPackageMsg.getSuccess()) {
Date now = new Date();
// 获取策略上架几天生效
int days = otaPackagePolicyPo.getEffectdaysAfterOnline();
OtaPackagePo data = otaPackageMsg.getData();
Date onLineTime = data.getOnLineTime();
// 当前时间在上架时间+天数之后返回true 生效
Calendar calendar = Calendar.getInstance();
calendar.setTime(onLineTime);
calendar.add(Calendar.DAY_OF_MONTH, days);
Date expirationDate = calendar.getTime();
return now.after(expirationDate);
}
return isEffect;
}
/**
* 是否满足生效时间
*/
public boolean isEffectAbsolute(OtaPackagePolicyPo otaPackagePolicyPo) {
boolean isEffect = false;
if (null == otaPackagePolicyPo.getExecuteStartTime()) {
return isEffect;
}
// 当前时间是否大于策略生效时间
Date now = new Date();
isEffect = now.after(otaPackagePolicyPo.getExecuteStartTime());
return isEffect;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
AllPolicyHandler.java
/**
* 全部升级策略处理器
*
* @author wangchao
* @create 2018-07-11 18:43
*/
@Component("allPolicyHandler")
public class AllPolicyHandler extends IPackagePolicyHandler {
@Override
public Message<String> matchPolicy(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
message.setSuccess(false);
// 校验是否达到生效时间
if (isEffect(otaPackagePolicyPo)) {
message.setSuccess(true);
return message;
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
@Override
public Message<String> matchPolicyAbsolute(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
// 校验是否达到生效时间
if (isEffectAbsolute(otaPackagePolicyPo)) {
message.setSuccess(true);
return message;
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
AreaPolicyHandler.java
/**
* 地区策略处理器 按照地区配置分批升级策略
*
* @author wangchao
* @create 2018-07-10 14:45
*/
@Component("areaPolicyHandler")
public class AreaPolicyHandler extends IPackagePolicyHandler {
// todo 根据经纬度范围直接确定地区 避免地址反解析耗时
@Override
public Message<String> matchPolicy(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
if (isEffect(otaPackagePolicyPo)) {
if (!StringUtils.isEmpty(check.getLat()) && !StringUtils.isEmpty(check.getLng())) {
List<Point> points = new ArrayList<>();
Point point = new Point();
// todo 坐标变换
//CoordinateUtil.bdGpsToGaode()
//纵坐标设置纬度
point.setY(Double.parseDouble(check.getLat()));
//横坐标设置经度
point.setX(Double.parseDouble(check.getLng()));
points.add(point);
GaodeGeocode gaodeGeocode = CoordinateUtil.gaodeRegeocode(points);
// 获取的反解析地址和数据库中对比 取城市代码 比较省级城市只需要比较前code的前两位
String adcode = "";
if (gaodeGeocode!=null && gaodeGeocode.getInfo().equalsIgnoreCase("OK") && gaodeGeocode.getRegeocodes().size() > 0) {
List<GaodeRegeocode> regeocodes = gaodeGeocode.getRegeocodes();
GaodeRegeocode gaodeRegeocode = regeocodes.get(0);
// 代码 如:500108 重庆市代码 500000
adcode = gaodeRegeocode.getAddressComponent().getAdcode().substring(0, 2) + "0000";
} else {
message.setMsg("地址解析异常");
return message;
}
// 校验是否在参数中
String policyParam = otaPackagePolicyPo.getPolicyParam();
if (policyParam.contains(adcode)) {
message.setSuccess(true);
return message;
}
} else {
message.setMsg("无法获取该设备的位置信息");
return message;
}
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
@Override
public Message<String> matchPolicyAbsolute(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
if (isEffectAbsolute(otaPackagePolicyPo)) {
if (!StringUtils.isEmpty(check.getLat()) && !StringUtils.isEmpty(check.getLng())) {
List<Point> points = new ArrayList<>();
Point point = new Point();
//纵坐标设置纬度
point.setY(Double.parseDouble(check.getLat()));
//横坐标设置经度
point.setX(Double.parseDouble(check.getLng()));
points.add(point);
GaodeGeocode gaodeGeocode = CoordinateUtil.gaodeRegeocode(points);
// 获取的反解析地址和数据库中对比 取城市代码 比较省级城市只需要比较前code的前两位
String adcode = "";
if (gaodeGeocode!=null && gaodeGeocode.getInfo().equalsIgnoreCase("OK") && gaodeGeocode.getRegeocodes().size() > 0) {
List<GaodeRegeocode> regeocodes = gaodeGeocode.getRegeocodes();
GaodeRegeocode gaodeRegeocode = regeocodes.get(0);
// 代码 如:500108 重庆市代码 500000
adcode = gaodeRegeocode.getAddressComponent().getAdcode().substring(0, 2) + "0000";
} else {
message.setMsg("地址解析异常");
return message;
}
// 校验是否在参数中
String policyParam = otaPackagePolicyPo.getPolicyParam();
if (policyParam.contains(adcode)) {
message.setSuccess(true);
return message;
}
} else {
message.setMsg("无法获取该设备的位置信息");
return message;
}
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
CarSeriesPolicyHandler.java
/**
* 车系升级策略
*
* @author wangchao
* @create 2018-07-12 17:43
*/
@Component("carseriesPolicyHandler")
public class CarSeriesPolicyHandler extends IPackagePolicyHandler {
@Autowired
private IGlobalUserCarService globalUserCarService;
@Autowired
private IBaseProductCarService baseProductCarService;
@Autowired
private ICoreTerminalDeviceService coreTerminalDeviceService;
public void setGlobalUserCarService(IGlobalUserCarService globalUserCarService) {
this.globalUserCarService = globalUserCarService;
}
public void setBaseProductCarService(IBaseProductCarService baseProductCarService) {
this.baseProductCarService = baseProductCarService;
}
public void setCoreTerminalDeviceService(ICoreTerminalDeviceService coreTerminalDeviceService) {
this.coreTerminalDeviceService = coreTerminalDeviceService;
}
@Override
public Message<String> matchPolicy(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
message.setSuccess(false);
// 校验是否达到生效时间
if (isEffect(otaPackagePolicyPo)) {
// 校验是否在参数中
Message<String> seriesCodeMsg = getSeriesCode(check);
if (seriesCodeMsg.getSuccess()) {
String seriesCode = seriesCodeMsg.getData();
String policyParam = otaPackagePolicyPo.getPolicyParam();
if (!StringUtils.isEmpty(policyParam)) {
String[] policyParams = policyParam.split(",");
for (String param : policyParams) {
if (param.equalsIgnoreCase(seriesCode)) {
message.setSuccess(true);
return message;
}
}
}
}
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
@Override
public Message<String> matchPolicyAbsolute(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
// 校验是否达到生效时间
if (isEffectAbsolute(otaPackagePolicyPo)) {
// 校验是否在参数中
Message<String> seriesCodeMsg = getSeriesCode(check);
if (seriesCodeMsg.getSuccess()) {
String seriesCode = seriesCodeMsg.getData();
String policyParam = otaPackagePolicyPo.getPolicyParam();
if (!StringUtils.isEmpty(policyParam)) {
String[] policyParams = policyParam.split(",");
for (String param : policyParams) {
if (param.equalsIgnoreCase(seriesCode)) {
message.setSuccess(true);
return message;
}
}
}
}
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
/**
* 获取车系 如:C301
*
* @param check
* @return
*/
private Message<String> getSeriesCode(UpgradeCheckParam check) {
Message<String> message = new Message<>();
message.setSuccess(false);
String seriesCode = null;
// 需要获取当前参数对应的车系
if (!StringUtils.isEmpty(check.getVin())) {
GlobalUserCarDTO globalUserCarDTO = new GlobalUserCarDTO();
globalUserCarDTO.setVin(check.getVin());
Message<GlobalUserCarDTO> globalUserCarMsg = globalUserCarService.getByParams(globalUserCarDTO);
if (globalUserCarMsg.getSuccess()) {
GlobalUserCarDTO data = globalUserCarMsg.getData();
seriesCode = data.getSeriesCode();
if (!StringUtils.isEmpty(seriesCode)) {
message.setSuccess(true);
message.setData(seriesCode);
return message;
}
} else {
BaseProductCarDTO baseProductCarDTO = new BaseProductCarDTO();
baseProductCarDTO.setVin(check.getVin());
Message<BaseProductCarDTO> baseProductCarMsg = baseProductCarService.getByParams(baseProductCarDTO);
if (baseProductCarMsg.getSuccess()) {
BaseProductCarDTO data = baseProductCarMsg.getData();
seriesCode = data.getSeriesCode();
if (!StringUtils.isEmpty(seriesCode)) {
message.setSuccess(true);
message.setData(seriesCode);
return message;
}
}
}
}
// tuid查core_terminal_device global_user_car
Message<CoreTerminalDeviceDTO> coreTerminalDeviceMsg = coreTerminalDeviceService.getByPrimaryKey(check.getTuid());
if (coreTerminalDeviceMsg.getSuccess()) {
CoreTerminalDeviceDTO msgData = coreTerminalDeviceMsg.getData();
String carId = msgData.getCarId();
if (!StringUtils.isEmpty(carId)) {
GlobalUserCarDTO globalUserCarDTO = new GlobalUserCarDTO();
globalUserCarDTO.setCarId(carId);
Message<GlobalUserCarDTO> globalUserCarMsg = globalUserCarService.getByParams(globalUserCarDTO);
if (globalUserCarMsg.getSuccess()) {
GlobalUserCarDTO globalUserCarMsgData = globalUserCarMsg.getData();
seriesCode = globalUserCarMsgData.getSeriesCode();
if (!StringUtils.isEmpty(seriesCode)) {
message.setSuccess(true);
message.setData(seriesCode);
return message;
}
}
}
}
message.setMsg("找不到所属车系");
return message;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
DeviceGroupPolicyHandler.java
/**
* 设备分组升级策略
*
* @author wangchao
* @create 2018-07-10 14:50
*/
@Component("devicegroupPolicyHandler")
public class DeviceGroupPolicyHandler extends IPackagePolicyHandler {
@Autowired
private IGlobalDeviceGroupService globalDeviceGroupService;
@Autowired
private IOtaDeviceGroupService otaDeviceGroupService;
public void setGlobalDeviceGroupService(IGlobalDeviceGroupService globalDeviceGroupService) {
this.globalDeviceGroupService = globalDeviceGroupService;
}
@Override
public Message<String> matchPolicy(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
// 校验是否达到生效时间
if (isEffect(otaPackagePolicyPo)) {
String policyParams = otaPackagePolicyPo.getPolicyParam();
String[] params = policyParams.split(",");
for (String param : params) {
GlobalDeviceGroupDTO globalDeviceGroup = new GlobalDeviceGroupDTO();
globalDeviceGroup.setDeviceId(check.getTuid());
globalDeviceGroup.setDeviceGroupId(param);
Message<GlobalDeviceGroupDTO> groupRetMsg = globalDeviceGroupService.getByParams(globalDeviceGroup);
if (groupRetMsg.getSuccess() || groupRetMsg.getCode() == 7) {
message.setSuccess(true);
return message;
}
}
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
@Override
public Message<String> matchPolicyAbsolute(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
// 校验是否达到生效时间
if (isEffectAbsolute(otaPackagePolicyPo)) {
String policyParams = otaPackagePolicyPo.getPolicyParam();
String[] params = policyParams.split(",");
for (String param : params) {
OtaDeviceGroupDTO otaDeviceGroupDTO = new OtaDeviceGroupDTO();
otaDeviceGroupDTO.setTuid(check.getTuid());
otaDeviceGroupDTO.setGroupItemId(param);
Message<OtaDeviceGroupDTO> groupRetMsg = otaDeviceGroupService.getByParams(otaDeviceGroupDTO);
if (groupRetMsg.getSuccess() || groupRetMsg.getCode() == 7) {
message.setSuccess(true);
return message;
}
}
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
TuidPolicyHandler.java
/**
* tuid包策略处理器 按照设备编号tuid升级策略
*
* @author wangchao
* @create 2018-07-10 14:39
*/
@Component("tuidPolicyHandler")
public class TuidPolicyHandler extends IPackagePolicyHandler {
@Override
public Message<String> matchPolicy(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
// 校验是否达到生效时间
if (isEffect(otaPackagePolicyPo)) {
// 校验是否在参数中
String policyParam = otaPackagePolicyPo.getPolicyParam();
if (policyParam.contains(check.getTuid())) {
message.setSuccess(true);
return message;
}
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
@Override
public Message<String> matchPolicyAbsolute(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
// 校验是否达到生效时间
if (isEffectAbsolute(otaPackagePolicyPo)) {
// 校验是否在参数中
String policyParam = otaPackagePolicyPo.getPolicyParam();
if (policyParam.contains(check.getTuid())) {
message.setSuccess(true);
return message;
}
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
PhonePolicyHandler.java
/**
* 手机号策略
*
* @author wangchao
* @create 2018-12-14 14:42
*/
@Component("phonePolicyHandler")
public class PhonePolicyHandler extends IPackagePolicyHandler {
@Override
public Message<String> matchPolicy(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
return message;
}
@Override
public Message<String> matchPolicyAbsolute(OtaPackagePolicyPo otaPackagePolicyPo, UpgradeCheckParam check) {
Message<String> message = new Message<>();
// 校验是否达到生效时间
if (isEffectAbsolute(otaPackagePolicyPo)) {
// 校验是否在参数中
String policyParam = otaPackagePolicyPo.getPolicyParam();
if (!StringUtils.isEmpty(check.getPhoneNumber())&&policyParam.contains(check.getPhoneNumber())) {
message.setSuccess(true);
return message;
}
}
message.setMsg("该设备不在本次设定的升级区域");
return message;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30